package com.ruoyi.web.controller.shop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberLabelDO;
import com.ruoyi.common.entity.VO.member.MemberLabelSearchVO;
import com.ruoyi.common.entity.VO.member.MemberLabelVO;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.web.controller.shop.mapper.IBackShopMemberLabelMapper;
import com.ruoyi.web.controller.shop.service.IBackShopMemberLabelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 会员标签标签管理
 */
@Controller
@RequestMapping("/back/shopMemberLabel")
public class BackShopMemberLabelController extends BaseController {
    private String prefix = "shop/member/label";

    @Resource
    private IBackShopMemberLabelService backShopMemberLabelService;

    @Resource
    private IBackShopMemberLabelMapper backShopMemberLabelMapper;

    /**
     * 会员标签列表-页面跳转
     *
     * @return
     */
    @RequiresPermissions("back:memberLabel:list")
    @GetMapping()
    public String list(ModelMap mmap) {
        return prefix + "/list";
    }

    /**
     * 查询会员标签列表-数据
     */
    @RequiresPermissions("back:memberLabel:list")
    @PostMapping("/searchList")
    @ResponseBody
    public TableDataInfo searchList(MemberLabelSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        startPage();
        searchVO.setShopId(ShiroUtils.getShopId());
        List<MemberLabelVO> list = backShopMemberLabelService.searchList(searchVO);
        return getDataTable(list);
    }

    /**
     * 数据查询-校验标签名称
     */
    @PostMapping("/checkLabelNameUnique")
    @ResponseBody
    public String checkLabelNameUnique(MemberLabelSearchVO searchVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        searchVO.setShopId(ShiroUtils.getShopId());
        return backShopMemberLabelService.checkLabelNameUnique(searchVO);
    }

    /**
     * 新增会员标签信息-页面跳转
     */
    @RequiresPermissions("back:memberLabel:link")
    @GetMapping("/link/{memberId}")
    public String link(@PathVariable("memberId") Long memberId, ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        List<MemberLabelVO> memberLabelList = backShopMemberLabelMapper.searchLinkList(memberId);
        MemberLabelSearchVO searchVO =new MemberLabelSearchVO();
        List<MemberLabelVO> memberLabelAllList = backShopMemberLabelMapper.searchList(searchVO);
        for (MemberLabelVO one:memberLabelAllList
             ) {
            for (MemberLabelVO one2:memberLabelList
                 ) {
                if(one.getId().equals(one2.getId())){
                    one.setSelected(true);
                }
            }
        }
        mmap.put("memberId",memberId);
        mmap.put("memberLabelList",memberLabelAllList);
        return prefix + "/link";
    }

    /**
     * 编辑会员标签-数据
     */
    @Log(title = "总后台-会员标签管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:memberLabel:link")
    @PostMapping("/link")
    @ResponseBody
    public AjaxResult link(MemberLabelVO MemberLabelVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        backShopMemberLabelService.link(MemberLabelVO);
        return success();
    }


    /**
     * 新增会员标签信息-页面跳转
     */
    @RequiresPermissions("back:memberLabel:add")
    @GetMapping("/add")
    public String add() {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        return prefix + "/add";
    }

    /**
     * 新增会员标签-数据
     */
    @Log(title = "总后台-会员标签管理", businessType = BusinessType.INSERT)
    @RequiresPermissions("back:memberLabel:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(MemberLabelVO MemberLabelVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        MemberLabelSearchVO searchVO = new MemberLabelSearchVO();
        searchVO.setLabelName(MemberLabelVO.getLabelName());
        searchVO.setShopId(ShiroUtils.getShopId());
        if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(backShopMemberLabelService.checkLabelNameUnique(searchVO))) {
            return error("新增标签'" + searchVO.getLabelName() + "'失败，标签名称已存在");
        }
        try {
            MemberLabelVO.setShopId(ShiroUtils.getShopId());
            MemberLabelVO.setCreateBy(ShiroUtils.getSysUser().getUserName());
            backShopMemberLabelService.add(MemberLabelVO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 查看会员标签信息-页面跳转
     */
    @RequiresPermissions("back:memberLabel:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        if (0 == ShiroUtils.getShopId()) {
            return "redirect:/back/shop/switchShop";
        }
        MemberLabelDO memberLabel = backShopMemberLabelService.selectById(id);
        mmap.put("memberLabel", memberLabel);
        return prefix + "/edit";
    }

    /**
     * 编辑会员标签-数据
     */
    @Log(title = "总后台-会员标签管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("back:memberLabel:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(MemberLabelVO MemberLabelVO) {
        if (0 == ShiroUtils.getShopId()) {
            throw new BusinessException("请先切换站点！");
        }
        MemberLabelSearchVO searchVO = new MemberLabelSearchVO();
        searchVO.setLabelId(MemberLabelVO.getId());
        searchVO.setLabelName(MemberLabelVO.getLabelName());
        searchVO.setShopId(ShiroUtils.getShopId());
        if (UserConstants.DEPT_NAME_NOT_UNIQUE.equals(backShopMemberLabelService.checkLabelNameUnique(searchVO))) {
            return error("修改标签'" + searchVO.getLabelName() + "'失败，标签名称已存在");
        }
        try {
            MemberLabelVO.setShopId(ShiroUtils.getShopId());
            MemberLabelVO.setUpdateBy(ShiroUtils.getSysUser().getUserName());
            MemberLabelVO.setUpdateTime(new Date());
            backShopMemberLabelService.edit(MemberLabelVO);
            return success();
        } catch (BusinessException e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除会员标签
     */
    @Log(title = "总后台-会员标签管理", businessType = BusinessType.DELETE)
    @RequiresPermissions("back:memberLabel:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(backShopMemberLabelService.removeByIds(ids));
    }

}
